﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeostatisticalTool.Classes;
namespace GeostatisticalTool.Forms
{
    public partial class SearchNeighborhoodControl : UserControl
    {

        public event EventHandler ChangedParameters;
        protected virtual void OnChangedParameters(EventArgs e)
        {
            if (ChangedParameters != null)
                ChangedParameters(this, e);
            if (search != null)
            {
                this.search.SetParameters(minN, maxN);
                this.search.SetAniso(az, minorR, majorR);
            }
        }

        public SearchNeighborhoodControl()
        {
            InitializeComponent();
            MaxN = 5;
            MinN = 2;
            az = 0;
            type = 1;
        }

        private SearchData search;

        public void SetSearch(SearchData search)
        {
            this.search = search;
        }

        public SearchData GetSearch()
        {
            return this.search;
        }

        public void SetParamenters(int maxN, int minN,double az, double majorR, double minorR, int type)
        {
            this.maxN = maxN;
            this.minN = minN;
            this.az = az;
            this.majorR = majorR;
            this.minorR = minorR;
            this.search.SetParameters(minN, maxN);
            this.search.SetAniso(az, minorR, majorR);
            this.search.Type = type;
            CalculateRatio();
        }

        private int maxN;

        public int MaxN
        {
            get { return maxN; }
            set { maxN = value;
                uxMaximumNeigh.Text = maxN.ToString();
            }
        }
        private int minN;

        public int MinN
        {
            get { return minN; }
            set { minN = value;
                uxMinimumNeigh.Text = minN.ToString();
            }
        }
        private double az;

        public double Az
        {
            get {
                return az;
            }
            set
            {
                uxAngle.Text = value.ToString();
                az = Utils.ToRad(value);
                 }
        }
        private double majorR;

        public double MajorR
        {
            get { return majorR; }
            set { majorR = value;
            uxMajorSemiAxis.Text = majorR.ToString();
            }
        }
        private double minorR;

        public double MinorR
        {
            get { return minorR; }
            set { minorR = value;
                    uxMinorSemiAxis.Text = minorR.ToString();

            }
        }
        private int type;

        public int Type
        {
            get {
                
                return type; }
            set { 
                type = value;

                if (type == 1) {
                    uxTypeSector.SelectedIndex = 0;
                }
                if (type == 4) {
                    uxTypeSector.SelectedIndex = 1;
                 }

                }
        }

        public void EnableApply(bool value)
        {
            uxApply.Enabled = value;
        }

        private void uxMaximumNeigh_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsInt32(uxMaximumNeigh))
            {
                maxN = Convert.ToInt32(uxMaximumNeigh.Text);
                uxApply.Enabled = true;
            }
         //   OnChangedParameters(EventArgs.Empty);

        }

        private void uxMinimumNeigh_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsInt32(uxMinimumNeigh))
            {
                minN = Convert.ToInt32(uxMinimumNeigh.Text);
                uxApply.Enabled = true;
            }
          //  OnChangedParameters(EventArgs.Empty);
        }

        private void uxMajorSemiAxis_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsDouble(uxMajorSemiAxis))
            {


                majorR = Convert.ToDouble(uxMajorSemiAxis.Text);
                uxApply.Enabled = true;
                CalculateRatio();
            }
          // OnChangedParameters(EventArgs.Empty);
        }

        private void uxTypeSector_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uxTypeSector.Text == "1 Sector")
                type= 1;
            if (uxTypeSector.Text == "4 Sectors")
                type= 4;
            uxApply.Enabled = true;
           // OnChangedParameters(EventArgs.Empty);
        }

        private void uxMinorSemiAxis_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsDouble(uxMinorSemiAxis))
            {
                minorR = Convert.ToDouble(uxMinorSemiAxis.Text);
                uxApply.Enabled = true;
                CalculateRatio();
            }
          //  OnChangedParameters(EventArgs.Empty);
        }

        private void CalculateRatio()
        { 
            if (minorR !=0 )
            { 
                double f= majorR/minorR;
                uxAnisotropyFactor.Text = Math.Round(f, 3).ToString();
           }
        }

        private void uxAngle_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsDouble(uxAngle))
            {
                az = Utils.ToRad(Convert.ToDouble(uxAngle.Text));
                uxApply.Enabled = true;
            }
          //  OnChangedParameters(EventArgs.Empty);
        }

        private void uxApply_Click(object sender, EventArgs e)
        {
            
            //SetParamenters(this.maxN, this.minN, this.az, this.majorR, this.minorR);
            if (this.maxN == 0)
            {
                if (this.minN > 0)
                    this.MinN = 0;
            }

            if (this.minN > this.maxN)
            {
                this.MinN = this.maxN;
               
            }


            if (this.majorR == 0 || this.minorR ==0)
                {
                    MessageBox.Show("Semiaxis should be greater than zero ");
                return;
                }
            
            this.search.SetParameters(minN, maxN);
            this.search.SetAniso(az, minorR, majorR);
            this.search.Type = this.type;
            OnChangedParameters(EventArgs.Empty);
            uxApply.Enabled = false;
        }

    }
}
